package com.wb.mas.ui.splash;

import android.app.Application;
import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;

import com.blankj.utilcode.util.SPUtils;
import com.wb.mas.R;
import com.wb.mas.app.SPKeys;
import com.wb.mas.data.DemoRepository;
import com.wb.mas.entity.GuideEntity;
import com.wb.mas.entity.RootApiBean;
import com.wb.mas.entity.VersionResponse;
import com.wb.mas.storage.AssetsUtils;
import com.wb.mas.ui.auth.AuthPersonalInfoAc;
import com.wb.mas.ui.guide.GuideActivity;
import com.wb.mas.ui.login.LoginAc;
import com.wb.mas.ui.main.MainActivity;
import com.wb.mas.ui.main.MainViewModel;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;
import me.goldze.mvvmhabit.base.BaseModel;
import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.bus.event.SingleLiveEvent;
import me.goldze.mvvmhabit.utils.ToastUtils;

/**
 * Copyright (C) 2018,
 * SplashViewModel
 * <p>
 * Description
 * 过渡页ViewModel
 *
 * @author mwl
 * @version 1.0
 * <p>
 * Ver 1.0, 2019-04-28, mwl, Create file
 */
public class SplashViewModel extends BaseViewModel<DemoRepository> {

    public UiChangeEvent uc = new UiChangeEvent();

    public VersionResponse.DataBean versionDateBean;

    public SplashViewModel(@NonNull Application application, DemoRepository model) {
        super(application, model);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        AssetsUtils.copyDB();
        isPrivacy();
    }

    public class UiChangeEvent {
        public SingleLiveEvent privacyEvent = new SingleLiveEvent();
    }


    public void isPrivacy() {
        if (!SPUtils.getInstance().getBoolean(SPKeys.SP_PRIVACY)) {
            uc.privacyEvent.setValue(new Object());
        } else {
            go();
        }
    }

    public void go() {
        addSubscribe(Observable.timer(1000, TimeUnit.MILLISECONDS)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Long>() {
                    @Override
                    public void accept(Long aLong) throws Exception {
                        if (SPUtils.getInstance().getBoolean(SPKeys.SP_FIRST, true)) {
                            SPUtils.getInstance().put(SPKeys.SP_FIRST, false);
                            startActivity(GuideActivity.class);
                            finish();
                        } else {
                            startActivity(MainActivity.class);
                            finish();
                        }
                    }
                })
        );
    }
}
